<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class ImagesController extends Controller
{
    public function uploadImage(Request $request)
    {
        $this->validate($request, [
            'image' => 'required|file',
        ]);
        if ($request->has('filename') && $request->input('filename')) {
            $path = Storage::disk('oss')->putFileAs('uat', $request->file('image'), $request->input('filename'));
        } else {
            $path = $request->file('image')->store('uat', 'oss');
        }
        $domain = 'https://' . config('filesystems.disks.oss.bucket') . '.' . config('filesystems.disks.oss.endpoint');
        $data = [
            'domain' => $domain,
            'path' => $path,
        ];

        return $this->success($data);
    }

    public function deleteImage(Request $request)
    {
        $this->validate($request, [
            'url' => 'required',
        ]);
        // $filePath = str_replace(config('filesystems.disks.cosv5.read_from_cdn'), '', $request->input('url'));
        $filePath = str_replace(config('filesystems.disks.oss.bucket') . config('filesystems.disks.oss.endpoint'), '', $request->input('url'));
        $res = Storage::disk('oss')->delete($filePath);

        if ($res) {
            Log::info('图片删除失败，图片地址：' . $filePath);
        }
        return $this->message('删除成功');
    }

}
